package com.gwtintegration.smartrfsandbox.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.shared.GWT;
import com.gwtintegration.smartrf.client.RequestFactoryDSRequest;
import com.gwtintegration.smartrfsandbox.client.gin.MainGinjector;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.FetchDataEvent;
import com.smartgwt.client.widgets.events.FetchDataHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTSmartRFSandbox implements EntryPoint {

	private final MainGinjector injector = GWT.create(MainGinjector.class);

	private ListGrid userGrid;
	private IButton newUserButton;
	private DynamicForm generalForm;
	private DynamicForm addressForm;

	private ValuesManager valuesManager;

	private TabSet tabSet;

	private IButton saveUserButton;

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {

		VLayout mainLayout = new VLayout();
		mainLayout.setHeight100();
		mainLayout.setWidth100();

		mainLayout.addMember(getUserGrid());
		mainLayout.addMember(getNewUserButton());

		mainLayout.addMember(getTabSet());
		mainLayout.addMember(getSaveUserButton());

		mainLayout.draw();
	}

	private ValuesManager getValuesManager() {
		if (valuesManager == null) {

			valuesManager = new ValuesManager();

			valuesManager.addMember(getGeneralForm());
			valuesManager.addMember(getAddressForm());

		}

		return valuesManager;
	}

	private ListGrid getUserGrid() {

		if (userGrid == null) {

			userGrid = new ListGrid();

			userGrid.setWidth(500);
			userGrid.setHeight(220);

			ListGridField idField = new ListGridField("id", "Id", 40);
			idField.setCanFilter(true);

			ListGridField nameField = new ListGridField("name", "Name", 100);
			idField.setCanFilter(true);

			ListGridField emailField = new ListGridField("email", "Email");
			idField.setCanFilter(true);

			userGrid.setShowFilterEditor(true);

			userGrid.setAutoFetchData(true);

			userGrid.addFetchDataHandler(new FetchDataHandler() {
				@Override
				public void onFilterData(FetchDataEvent event) {
					RequestFactoryDSRequest request = new RequestFactoryDSRequest(event.getRequestProperties());
					request.setFetchWith("profiles");
				}
			});
		}
		return userGrid;
	}

	private TabSet getTabSet() {
		if (tabSet == null) {
			tabSet = new TabSet();

			Tab generalTab = new Tab("General");
			generalTab.setPane(getGeneralForm());

			Tab addressTab = new Tab("Address");
			addressTab.setPane(getAddressForm());

			tabSet.addTab(generalTab);
			tabSet.addTab(addressTab);
		}

		return tabSet;
	}

	private DynamicForm getGeneralForm() {

		if (generalForm == null) {
			generalForm = new DynamicForm();

			TextItem name = new TextItem("name", "Name");
			TextItem email = new TextItem("email", "Email");
			PasswordItem password = new PasswordItem("password", "Password");
			PasswordItem confirmPassword = new PasswordItem("confirmPassword", "Confirm Password");

			generalForm.setFields(name, email, password, confirmPassword);
		}
		return generalForm;
	}

	private DynamicForm getAddressForm() {
		if (addressForm == null) {
			addressForm = new DynamicForm();

			addressForm.setDataPath("address");

			TextItem street = new TextItem("street", "Street");
			TextItem number = new TextItem("number", "Number");

			addressForm.setFields(street, number);
		}

		return addressForm;
	}

	private IButton getSaveUserButton() {
		if (saveUserButton == null) {
			saveUserButton = new IButton("Save User");
			saveUserButton.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {

					RequestFactoryDSRequest request = new RequestFactoryDSRequest(DSOperationType.ADD);
					request.setFetchWith("profiles");

					getValuesManager().saveData(new DSCallback() {
						@Override
						public void execute(DSResponse response, Object rawData, DSRequest request) {
							if (response.getStatus() == RPCResponse.STATUS_SUCCESS) {
								getValuesManager().editNewRecord();
							}
						}
					}, request);
				}
			});
		}

		return saveUserButton;
	}

	private IButton getNewUserButton() {

		if (newUserButton == null) {
			newUserButton = new IButton("New User");
			newUserButton.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					getValuesManager().editNewRecord();
				}
			});
		}
		return newUserButton;
	}
}
